package com.ruoyi.restaurant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.restaurant.domain.Order;
import com.ruoyi.restaurant.domain.OrderDetail;
import com.ruoyi.restaurant.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 订单Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/restaurant/order")
public class OrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 根据餐台ID查询进行中的订单
     */
    @GetMapping("/table/{tableId}")
    public AjaxResult getOrderByTableId(@PathVariable("tableId") Long tableId) {
        return success(orderService.selectOrderByTableId(tableId));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单
     */
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     * 点单（创建订单并添加菜品）
     */
    @Log(title = "点单", businessType = BusinessType.INSERT)
    @PostMapping("/place")
    public AjaxResult placeOrder(@RequestBody Order order) {
        return toAjax(orderService.placeOrder(order));
    }

    /**
     * 加单（向现有订单添加菜品）
     */
    @Log(title = "加单", businessType = BusinessType.UPDATE)
    @PostMapping("/add-items")
    public AjaxResult addOrderItems(@RequestBody List<OrderDetail> orderDetails) {
        return toAjax(orderService.addOrderItems(orderDetails));
    }

    /**
     * 查询订单明细
     */
    @GetMapping("/details/{orderId}")
    public AjaxResult getOrderDetails(@PathVariable("orderId") Long orderId) {
        return success(orderService.selectOrderDetailList(orderId));
    }

    /**
     * 结账
     */
    @Log(title = "结账", businessType = BusinessType.UPDATE)
    @PostMapping("/checkout")
    public AjaxResult checkout(@RequestBody Order order) {
        return toAjax(orderService.checkout(order));
    }

    /**
     * 查询已结账订单列表（订单中心）
     */
    @GetMapping("/checked-out")
    public TableDataInfo getCheckedOutOrders(Order order) {
        startPage();
        List<Order> list = orderService.selectCheckedOutOrderList(order);
        return getDataTable(list);
    }

    /**
     * 查询订单详情（包含订单明细）
     */
    @GetMapping("/detail/{orderId}")
    public AjaxResult getOrderDetail(@PathVariable("orderId") Long orderId) {
        return success(orderService.selectOrderDetailById(orderId));
    }

    /**
     * 查询待出餐菜品（出餐中心）
     */
    @GetMapping("/kitchen/pending")
    public AjaxResult getPendingKitchenItems(@RequestParam(required = false) String categoryType) {
        return success(orderService.selectPendingKitchenItems(categoryType));
    }

    /**
     * 按订单分组查询待出餐菜品（出餐中心新版）
     */
    @GetMapping("/kitchen/pending-by-order")
    public AjaxResult getPendingKitchenItemsByOrder(@RequestParam(value = "categoryTypes", required = false) String[] categoryTypes) {
        List<String> typeList = categoryTypes != null ? Arrays.asList(categoryTypes) : null;
        return success(orderService.selectPendingKitchenItemsByOrder(typeList));
    }

    /**
     * 标记菜品为已出餐
     */
    @Log(title = "出餐", businessType = BusinessType.UPDATE)
    @PutMapping("/kitchen/serve/{id}")
    public AjaxResult markItemAsServed(@PathVariable("id") Long id) {
        return toAjax(orderService.markItemAsServed(id));
    }

    /**
     * 查询已出餐菜品记录（出餐管理）
     */
    @GetMapping("/kitchen/served")
    public TableDataInfo getServedKitchenItems(
            @RequestParam(required = false) String orderNumber,
            @RequestParam(required = false) String tableNumber,
            @RequestParam(required = false) String categoryType,
            @RequestParam(required = false) String beginTime,
            @RequestParam(required = false) String endTime) {
        startPage();
        java.util.Map<String, Object> params = new java.util.HashMap<>();
        params.put("orderNumber", orderNumber);
        params.put("tableNumber", tableNumber);
        params.put("categoryType", categoryType);
        if (beginTime != null && endTime != null) {
            java.util.Map<String, Object> timeParams = new java.util.HashMap<>();
            timeParams.put("beginTime", beginTime);
            timeParams.put("endTime", endTime);
            params.put("params", timeParams);
        }
        List<java.util.Map<String, Object>> list = orderService.selectServedKitchenItems(params);
        return getDataTable(list);
    }

    /**
     * 按餐台分组查询出餐记录
     */
    @GetMapping("/kitchen/records")
    public TableDataInfo getKitchenRecordsByTable(
            @RequestParam(required = false) String orderNumber,
            @RequestParam(required = false) String tableNumber,
            @RequestParam(required = false) String kitchenStatus,
            @RequestParam(required = false) String beginTime,
            @RequestParam(required = false) String endTime) {
        startPage();
        java.util.Map<String, Object> params = new java.util.HashMap<>();
        params.put("orderNumber", orderNumber);
        params.put("tableNumber", tableNumber);
        params.put("kitchenStatus", kitchenStatus);
        if (beginTime != null && endTime != null) {
            java.util.Map<String, Object> timeParams = new java.util.HashMap<>();
            timeParams.put("beginTime", beginTime);
            timeParams.put("endTime", endTime);
            params.put("params", timeParams);
        }
        List<java.util.Map<String, Object>> list = orderService.selectKitchenRecordsByTable(params);
        return getDataTable(list);
    }

    /**
     * 查询指定订单的所有菜品明细
     */
    @GetMapping("/kitchen/details/{orderId}")
    public AjaxResult getOrderKitchenDetails(@PathVariable("orderId") Long orderId) {
        return success(orderService.selectOrderKitchenDetails(orderId));
    }
}

